<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小同住人员物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\carwash;

use think\App;
use app\controller\property\Base;
use think\exception\ValidateException;
use app\validate\carwash\CarwashLabelValidate;
use app\common\repositories\carwash\CarwashLabelRepository;


class CarwashLabel extends Base
{
    protected $repository;

    public function __construct(App $app, CarwashLabelRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
        $this->assign([
            'addAuth' => property_auth('propertyCarwashLabelAdd'),
            'editAuth' => property_auth('propertyCarwashLabelEdit'),
            'delAuth' => property_auth('propertyCarwashLabelDel'),
            'detailAuth' => property_auth('propertyCarwashLabelDetail'),
        ]);
    }

    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'keywords' => '',
            ]);
            [$page, $limit] = $this->getPage();
            $res = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $res['list'], 'count' => $res['count']]);
        }
        return $this->fetch('carwash/label/list');
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'label_name' => '',
                'is_show' => '',
                'remark' => '',
            ]);
            validate(CarwashLabelValidate::class)->scene('add')->check($param);
            if ($this->repository->fieldExists('label_name', $param['label_name'])) {
                return $this->error($param['label_name'] . ' 已存在');
            }
            try {
                $res = $this->repository->addInfo($param, $this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加洗车站店标签失败', $e);
                return $this->error('网络失败');
            }
        } else {
            return $this->fetch('carwash/label/add', [

            ]);
        }

    }

    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'label_name' => '',
                'is_show' => '',
                'remark' => '',
            ]);
            validate(CarwashLabelValidate::class)->scene('edit')->check($param);
            if ($this->repository->propertyFieldExists($this->request->propertyId, 'label_name', $param['label_name'], $id)) {
                return $this->error($param['label_name'] . ' 已存在');
            }
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('修改洗车站店标签失败', $e);
                return $this->error('网络错误');
            }
        } else {
            return $this->fetch('carwash/label/edit', [
                'info' => $info,
            ]);
        }
    }

    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除洗车站店标签失败', $e);
            return $this->error('网络失败');
        }
    }


    public function detail()
    {
        $labelId = (int)$this->request->param('label_id');
        if ($labelId <= 0) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($labelId);
        if ($info['id'] <= 0) {
            return $this->error('信息不存在');
        }
        return $this->fetch('carwash/label/detail', [
            'info' => $info
        ]);
    }

}